#import "BXPressureView.h"
#import "BXContainer.h"

@interface BXPressureView (Private) 

- (NSRect)_rectForPointerAtLocation:(NSPoint)location withPressure:(float)pressure ;
- (void)_pointerChanged:(NSEvent *)event ;

- (void)_startLassoSelectionWithEvent:(NSEvent *)event ;

@end

@implementation BXPressureView

// Private
- (NSRect)_rectForPointerAtLocation:(NSPoint)location withPressure:(float)pressure {
	float pointerRadius = pressure *50 ;

	return NSMakeRect(location.x -pointerRadius, location.y -pointerRadius, pointerRadius *2, pointerRadius *2) ;
}

- (void)_startLassoSelectionWithEvent:(NSEvent *)event {
	NSRect dirtyRect = NSZeroRect ;
	
	_pointerOrigin = [self convertPoint:[event locationInWindow] fromView:nil] ;
	
	[_pointerPath moveToPoint:_pointerOrigin] ;
	
		while(event = [[self window] nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSPeriodicMask)]) {
			_pointerLocation = [self convertPoint:[[self window] mouseLocationOutsideOfEventStream] fromView:nil] ;
			_pointerPressure = [event pressure] ;	
			dirtyRect = _pointerRect ;
			_pointerRect = [self _rectForPointerAtLocation:_pointerLocation withPressure:_pointerPressure] ;
			dirtyRect = NSUnionRect(dirtyRect, _pointerRect) ;
			
			[_pointerPath lineToPoint:_pointerLocation] ;
			[self setNeedsDisplayInRect:NSInsetRect(NSUnionRect(dirtyRect, [_pointerPath bounds]), -2, -2)] ;
			
			//[self setNeedsDisplayInRect:NSInsetRect(NSUnionRect(_selectionRect, previousSelectionRect), -2, -2)] ;
				
//				if([self autoscroll:event]) {
//						if(generatesPeriodicEvents == NO) {
//							[NSEvent startPeriodicEventsAfterDelay:0.0 withPeriod:0.05] ;
//							generatesPeriodicEvents = YES ;
//						}
//				} else {
//					[NSEvent stopPeriodicEvents] ;
//					generatesPeriodicEvents = NO ;
//				}

				if([event type] == NSLeftMouseUp) {
					break ;	
				}
		}
		
	[_pointerPath removeAllPoints] ;
	[self setNeedsDisplay:YES] ;
}

// NSView
- (id)initWithFrame:(NSRect)frame {
	self = [super initWithFrame:frame] ;
	
	_pointerOrigin = NSZeroPoint ;
	_pointerLocation = NSZeroPoint ;
	_pointerPressure = 0.0 ;
	_pointerRect = NSZeroRect ;
	_pointerPath = [[NSBezierPath alloc] init] ;	
	//
	return self ;
}

- (void)drawRect:(NSRect)rect {
	[[NSColor lightGrayColor] set] ;
	[NSBezierPath fillRect:rect] ;

	NSEnumerator *containerEnumerator = [[dataSource arrangedObjects] objectEnumerator] ;
	BXContainer *currentContainer = nil ;
	
	[[NSColor redColor] set] ;
	
		while(currentContainer = [containerEnumerator nextObject]) {
			 [NSBezierPath fillRect:[currentContainer frame]] ;
		}

	[[NSColor whiteColor] set] ;
	[_pointerPath stroke] ;
	
	[[[NSColor whiteColor] colorWithAlphaComponent:0.666] set] ;
	[_pointerPath fill] ;
	
		if(![_pointerPath isEmpty]) {
			[NSBezierPath strokeLineFromPoint:_pointerOrigin toPoint:[_pointerPath currentPoint]] ;
		}
		
	[[NSColor blueColor] set] ;
	[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(_pointerOrigin.x -20, _pointerOrigin.y -20, 40, 40)] fill] ;	

	[[[NSColor whiteColor] colorWithAlphaComponent:_pointerPressure] set] ;
	[[NSBezierPath bezierPathWithOvalInRect:_pointerRect] fill] ;
}

- (void)resetCursorRects {
	[self addCursorRect:[self bounds] cursor:[NSCursor crosshairCursor]] ;
}

// NSResponder
- (void)mouseDown:(NSEvent *)event {
//	NSLog(@"mouseDown") ;

//	_pointerOrigin = [self convertPoint:[event locationInWindow] fromView:nil] ;
//	
//	[_pointerPath moveToPoint:_pointerOrigin] ;
//	
//	[self _pointerChanged:event] ;
}

//- (void)mouseMoved:(NSEvent *)event {
//	[self _pointerChanged:event] ;
//}
//
- (void)mouseDragged:(NSEvent *)event {
	[self _startLassoSelectionWithEvent:event] ;
}
//
- (void)mouseUp:(NSEvent *)event {
	//NSLog(@"mouseUp") ;
	
	NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil] ;
	
	[dataSource addObject:[BXContainer containerWithOrigin:location size:NSMakeSize(30, 30)]] ;
	
	[self setNeedsDisplay] ;

//	NSRect dirtyRect = [_pointerPath bounds] ;
//	[_pointerPath removeAllPoints] ;
//	[self setNeedsDisplayInRect:dirtyRect] ;
//
//	[self _pointerChanged:event] ;
}

//- (void)tabletProximity:(NSEvent *)event {
//	NSLog(@"tablet proximity") ;
//}
//
//- (void)tabletPoint:(NSEvent *)event {
//	NSLog(@"tabletPoint") ;
//}

//- (void)flagsChanged:(NSEvent *)event {
//	NSLog(@"flags changed") ;
//}

- (BOOL)acceptsFirstResponder {
	return YES ;
}

// NSObject
- (void)awakeFromNib {
	[[self window] setAcceptsMouseMovedEvents:YES] ;
	[[self window] makeFirstResponder:self] ;
}

- (void)dealloc {
	[_pointerPath release] ;

	[super dealloc] ;
}

@end